﻿using System;
using System.Windows;
using Kiva7.DataClasses;
using Kiva7.Translations;
using Kiva7.Utilities;
using Kiva7.DataRequests;
using Kiva7.ViewModels;
using Kiva7.Images;

namespace Kiva7.Pages
{
    public partial class FriendAddPage
    {
        private const string Url = "/Pages/FriendAddPage.xaml";
        public static readonly Uri PageUrl = new Uri(Url, UriKind.RelativeOrAbsolute);
        private const string LenderIdKey = "LenderId";

        public FriendAddPage()
        {
            InitializeComponent();
            CreateStandardControls();
            request = new LenderRequest();

            DataContext = request;
        }

        public static Uri GetPageUrl(string lenderId)
        {
            if (lenderId == null)
                return null;


            UrlCreator url = new UrlCreator(Url);
            url.Add(LenderIdKey, lenderId);

            return url.ToUri(UriKind.RelativeOrAbsolute);
        }


        protected override void CreateApplicationBar()
        {
            AddBarButton(AppResources.Save, ImageUrls.Save, Button_Click);
        }

        
        LenderRequest request;

        private void LenderID_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            request.GetLender(LenderID.Text);
        }

        private void Button_Click(object sender, EventArgs e)
        {
            if (DataController.ViewModel.Friends.Add(
                new Friend()
                {
                    LenderId = LenderID.Text
                }))
            {
                
            }
            NavigationService.GoBack();
        }

        private void BasePhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            string t = NavigationContext.GetQueryString(LenderIdKey);
            if (!string.IsNullOrEmpty(t))
            {
                LenderID.Text = t;    
            }
        }
    }
}